package com.tqjc.controller;

import com.tqjc.entity.PublicResult;
import com.tqjc.work.CraneWork;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;



@Controller
@RequestMapping("/craneWork")
public class CraneController {
    @Resource
    CraneWork craneWork;

    @RequestMapping("/onlineWork")
    public String onlineWork() {
        return "xg5msteel/craneWork/onlineTask.html";
    }

    @RequestMapping("/offlineWork")
    public String offlineWork() {
        return "xg5msteel/craneWork/offlineTask.html";
    }

    @RequestMapping("/splitFlowWork")
    public String splitFlowWork() {
        return "xg5msteel/craneWork/splitFlowTask.html";
    }


    @RequestMapping("/testBackground")
    public String testBackground() {
        return "xg5msteel/TestBackground.html";
    }


    //启动上线工作
    @GetMapping("/onlineTask/start")
    public PublicResult onlineStartTask(String onlinePath){
        PublicResult status=checkCraneWorkStatus("online");
        if(status.isSuccess()){
            return craneWork.onlineStartWork(onlinePath);
        }else {
            return status;
        }
    }

    //停止上线工作
    @GetMapping("/onlineTask/stop")
    public PublicResult onlineStopTask(String onlinePath){

        return null;
    }

    //启动下线工作
    @GetMapping("/offlineTask/start")
    public PublicResult offlineStartTask(String offlinePath){
        PublicResult status=checkCraneWorkStatus("offline");
        if(status.isSuccess()){
            return craneWork.offlineStartWork(offlinePath);
        }else {
            return status;
        }
    }


    //停止下线工作
    @GetMapping("/offlineTask/stop")
    public PublicResult offlineStopTask(String offlinePath){
        PublicResult status=checkCraneWorkStatus("offline");
        if(status.isSuccess()){
            return craneWork.offlineStopWork(offlinePath);
        }else {
            return status;
        }
    }

    //启动分流工作
    @GetMapping("/splitFlowTask/start")
    public PublicResult splitFlowStartTask(String splitFlowPath){
        PublicResult status=checkCraneWorkStatus("splitFlow");
        if(status.isSuccess()){
            return craneWork.splitFlowStartWork(splitFlowPath);
        }else {
            return status;
        }
    }


    //停止分流工作
    @GetMapping("/splitFlowTask/stop")
    public PublicResult splitFlowStopTask(String splitFlowPath){
        PublicResult status=checkCraneWorkStatus("splitFlow");
        if(status.isSuccess()){
            return craneWork.splitFlowStopWork(splitFlowPath);
        }else {
            return status;
        }
    }

    private PublicResult checkCraneWorkStatus(String workType){
        if("online".equals(workType)){
            if(craneWork.getOnlineWorking()){
                return PublicResult.fail("正在进行上线作业，请不要重复操作");
            }
            if(craneWork.getOfflineWorking()){
                return PublicResult.fail("正在进行下线作业，先停止再操作");
            }
            if(craneWork.getSplitFlowWorking()){
                return PublicResult.fail("正在进行分流作业，先停止再操作");
            }
        } else if ("offline".equals(workType)) {
            if(craneWork.getOnlineWorking()){
                return PublicResult.fail("正在进行上线作业，先停止再操作");
            }
            if(craneWork.getOfflineWorking()){
                return PublicResult.fail("正在进行下线作业，请不要重复操作");
            }
            if(craneWork.getSplitFlowWorking()){
                return PublicResult.fail("正在进行分流作业，先停止再操作");
            }
        }else if ("splitFlow".equals(workType)) {
            if(craneWork.getOnlineWorking()){
                return PublicResult.fail("正在进行上线作业，先停止再操作");
            }
            if(craneWork.getOfflineWorking()){
                return PublicResult.fail("正在进行下线作业，先停止再操作");
            }
            if(craneWork.getSplitFlowWorking()){
                return PublicResult.fail("正在进行分流作业，请不要重复操作");
            }
        }
        return PublicResult.ok();
    }

}
